﻿package com.snzflash.components.display 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import gs.TweenLite;
	
	/**
	 * Простой класс обертка для лоадера изображений
	 * @author firsoff maxim, icq:235859730, skype:maxim.firsoff, tel:+79222345364
	 */
	public class Image extends Loader 
	{
		public var autosize:Boolean = false;
		public var autoWidth:Number;
		public var autoHeight:Number;
		public var smoothing:Boolean = true;
		public var useFade:Boolean = true;
		public var checkPolicyFile:Boolean;
		
		protected var _request:URLRequest;
		protected var _fadeDuration:Number = 0.5;
		protected var _unscaledWidth:Number
		protected var _unscaledHeight:Number
		protected var _data:Object;
		
		public function Image() 
		{
			super();
		}
		
		public function loadURL(url:String):void 
		{
			scaleX = scaleY  = 1;
			TweenLite.killTweensOf(this);
			try {close();}
			catch (e:Error) { };
			
			try { unload(); }
			catch (e:Error) { };
			
			_request = new URLRequest(url);
			contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
			
			if (useFade) alpha = 0;
			
			if (checkPolicyFile)
			{
				var lc:LoaderContext = new LoaderContext(true);
				lc.checkPolicyFile = true;
				load(_request, lc);
			}
			else 
			{
				load(_request);
			}
			
			
			
			////trace('loadURL : ' + url);
		}
		
		
		public function switchTo(url:String, switchDuration:Number = 0.5):void 
		{
			switchDuration = Math.abs(switchDuration);
			TweenLite.killTweensOf (this);
			TweenLite.to (this, switchDuration, { alpha : 0, onComplete:onSwitch, onCompleteParams:[url] } );
		};
		
		private function onSwitch(...params:Array):void
		{
			loadURL(params[0]);
			//alpha = 1;
		};
		
		protected function onError(e:IOErrorEvent):void 
		{
			dispatchEvent(e.clone());
		}
		
		public function destroy():void 
		{
			contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			try {close();}
			catch (e:Error) { };
			
			try { unload(); }
			catch (e:Error) { };
		}
		
		protected function onComplete(event:Event):void 
		{
			event.stopImmediatePropagation();
			contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			
			////trace(content);
			if (content) 
			{
				try {Bitmap(content).smoothing = smoothing;}
				catch (err:Error) { };
			}
			
			_unscaledHeight = contentLoaderInfo.height;
			_unscaledWidth = contentLoaderInfo.width;
			
			if (useFade)  { TweenLite.to(this, _fadeDuration, { alpha:1} ); }
			else { alpha = 1;}
			
			if (autosize && autoHeight && autoWidth) scaleX = scaleY = Math.min(autoWidth / width , autoHeight / height);
			dispatchEvent(event.clone());
		}
		
		
		
		protected function onProgress(event:ProgressEvent):void 
		{
			dispatchEvent(event.clone());
		}
		
		public function get data():Object { return _data; }
		
		public function set data(value:Object):void 
		{
			_data = value;
		}
		
		public function get unscaledWidth():Number { return _unscaledWidth; };
		
		public function get unscaledHeight():Number { return _unscaledHeight; };
		
		public function get fadeDuration():Number { return _fadeDuration; };
		
		public function set fadeDuration(value:Number):void 
		{
			_fadeDuration = value;
		};
		
	}
	
}